package ifi.p17.jarmanagement.view;

import java.util.ArrayList;

import ifi.p17.jarmanagement.model.Purchase;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class CustomListAdapter extends ArrayAdapter<Purchase>{

	ArrayList<Purchase> array;
	int resource;
	Context context;
	
	public CustomListAdapter(Context context, int textViewResourceId,
			ArrayList<Purchase> objects) {
		super(context, textViewResourceId, objects);
		
		this.array = objects;
		this.context = context;
		resource = textViewResourceId;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View purView = convertView;
		
		if (purView == null) purView = new CustomViewItemList(getContext());
		
		final Purchase pur = array.get(position);
		
		if (pur != null) {
			TextView tv_tvj_content = ((CustomViewItemList) purView ).getTv_tvj(); 
			TextView tv_des_content = ((CustomViewItemList) purView ).getTv_des();
			CheckBox check = ((CustomViewItemList) purView ).getCb();
			
			check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					pur.setChecked(isChecked);
				}
			});
			
			tv_tvj_content.setText(pur.infoLine1());
			tv_des_content.setText(pur.infoLine2());
			check.setChecked(pur.isChecked());
		}
		
		return purView;
	}

}
